package com.xiaoyu.merchant.ui.fragment.goods;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xiaoyu.commonlib.network.CommonHttpResponser;
import com.xiaoyu.commonlib.ui.base.BaseFragment;
import com.xiaoyu.commonlib.utils.CCircleImageView;
import com.xiaoyu.commonlib.utils.PreferencesUtil;
import com.xiaoyu.commonlib.utils.ToastUtil;
import com.xiaoyu.merchant.MerchantConstants;
import com.xiaoyu.merchant.R;
import com.xiaoyu.merchant.loader.BannerImageLoader;
import com.xiaoyu.merchant.network.NetworkManager;
import com.xiaoyu.merchant.ui.activity.PhotoViewActivity;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 商品详情页中的详情fragment
 */
public class GoodsDetailFragment extends BaseFragment {
    @BindView(R.id.fragment_goods_detail_banner)
    Banner mBanner;//轮播图
    @BindView(R.id.fragment_goods_detail_name)
    TextView goodsName;//商品名称
    @BindView(R.id.fragment_goods_detail_price)
    TextView goodsPrice;//价格
    @BindView(R.id.fragment_goods_detail_freight)
    TextView mFreight;//运费
    @BindView(R.id.fragment_goods_detail_store_img)
    CCircleImageView mStoreImg;//商铺图片
    @BindView(R.id.fragment_goods_detail_store_name)
    TextView mStoreName;//商铺名
    @BindView(R.id.detail_content_txt)
    TextView mContentTxt;//商品文字描述
    @BindView(R.id.detail_img_layout)
    LinearLayout mImgLayout;//商品图片描述容器
    private View mView;

    private List<String> mBannerList = new ArrayList<>();//轮播图数据
    private List<String> mImgList = new ArrayList<>();//商品图片描述
    private String goodsId;//商品id
    private String storeId;//商家id
//    private GoodsDetailBean.DataBean goodsModel;//商品model

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        goodsId = bundle.getString(MerchantConstants.GOODS_ID);
        getGoodsInfo();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mView == null) {
            mView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_goods_detail, container, false);
            ButterKnife.bind(this, mView);

            initView();
        }
        ViewGroup parent = (ViewGroup) mView.getParent();
        if (parent != null) {
            parent.removeView(mView);
        }
        return mView;
    }


    private void initView() {
        //顶部轮播图
        mBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        mBanner.setIndicatorGravity(BannerConfig.CENTER);
        //设置是否自动轮播（不设置则默认自动）
        mBanner.isAutoPlay(true);
        //设置是否允许手动滑动轮播图
        mBanner.setViewPagerIsScroll(true);
        //设置轮播图片间隔时间（不设置默认为2000）
        mBanner.setDelayTime(2000);
        mBanner.setImageLoader(new BannerImageLoader());
        mBanner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                Intent intent = new Intent(getActivity(), PhotoViewActivity.class);
                intent.putExtra(MerchantConstants.IMAGER_LIST_URL, (Serializable) mBannerList);
                intent.putExtra(MerchantConstants.IMAGER_CLICK_POS, position);
                startActivity(intent);
            }
        });

        mBanner.setFocusable(true);
        mBanner.setFocusableInTouchMode(true);
        mBanner.requestFocus();


    }

    /**
     * 选择规格
     */
    @OnClick(R.id.fragment_goods_detail_specification_id)
    public void specificationClick() {
//        SpecificationDialog dialog = new SpecificationDialog(getActivity(), true, true);
//        dialog.initData(goodsModel);
//        dialog.show();
    }



    /**
     * 获取商品信息
     */
    private void getGoodsInfo() {
        NetworkManager.goodsDetails(PreferencesUtil.getString(PreferencesUtil.TOKEN, ""), goodsId, new CommonHttpResponser() {
            @Override
            public void onSuccess(String result) {
                parseGoodsInfoSuc(result);
            }

            @Override
            public void onFailed(String errorCode, String errorDiscription) {
                ToastUtil.logError("商品详情", errorCode, errorDiscription);
            }
        });
    }

    private void parseGoodsInfoSuc(String result) {
        ToastUtil.logResult("商品详情", result);
//        GoodsDetailBean bean = new Gson().fromJson(result, GoodsDetailBean.class);
//        goodsModel = bean.getData();
//        //轮播图
//        mBannerList.clear();
//        mBannerList.addAll(bean.getData().getGoodslbimg());
//        mBanner.setImages(mBannerList);
//        mBanner.start();
//        //商品
//        goodsName.setText(bean.getData().getGoodsname());//名称
//        goodsPrice.setText("¥" + bean.getData().getMinmoney() + "-" + bean.getData().getMaxmoney());//价格区间
//        mFreight.setText(bean.getData().getGoodsfreight());//运费
//        Glide.with(this).load(bean.getData().getStore().getStoreimg()).into(mStoreImg);//店铺头像
//        mStoreName.setText(bean.getData().getStore().getStorename());//店铺名称
//
//        //文字描述
//        mContentTxt.setText(bean.getData().getGoodscontent());
//        //图片描述
//        mImgList.clear();
//        mImgList.addAll(bean.getData().getGoodsxqimg());
//        for (int i = 0; i < mImgList.size(); i++) {
//            ImageView view = new ImageView(getActivity());
//            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//            view.setLayoutParams(params);
//            Glide.with(getActivity()).load(mImgList.get(i)).into(view);
//            mImgLayout.addView(view, i);
//        }
//
//        storeId = bean.getData().getStore().getGoodsstroreid();
    }

}
